<?php
require_once "lib/nusoap.php";

$my_url = 'http://localhost/fashion_services/';
$hostname="1.234.91.160";
$username="cam";
$password="5678";
$databasename="fashion";
//echo date("Y-m-d");
function LogInMember($user, $pass) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$user=urldecode($user);
	$result=mysql_query("SELECT * FROM member m WHERE (m.mem_id='$user' AND m.mem_pswd='$pass' AND m.status='1')");


	if(mysql_num_rows($result)>0){
		$r = mysql_fetch_assoc($result);
		mysql_close($conn);
		return json_encode($r);
	}else{
		mysql_close($conn);
		return '{}';
	}
}

function GetInfoMember($user) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$user=urldecode($user);
	$result=mysql_query("SELECT * FROM member m WHERE (m.mem_id='$user' AND m.status='1')");



	if(mysql_num_rows($result)>0){
		$r = mysql_fetch_assoc($result);
		mysql_close($conn);
		return  json_encode($r);
	}else{
		mysql_close($conn);
		return  '{}';
	}
}

function ChangePasswordMember($user, $passOld, $passNew) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$user=urldecode($user);
	$result=mysql_query("UPDATE member SET mem_pswd='$passNew' WHERE (mem_id='$user' AND mem_pswd='$passOld' AND status='1')");

	$rs=mysql_affected_rows();

	mysql_close($conn);
	return $rs;
}

function CreateNewMember($user, $pass, $alias,$pic, $profile, $country) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	$user=urldecode($user);
	$alias=urldecode($alias);
	$profile=urldecode($profile);
	
	$check=mysql_query("SELECT * FROM member m WHERE m.mem_id='$user'");
	if(mysql_num_rows($check)>0){
		mysql_close($conn);
		return  '-1';
	}else{
		$result=mysql_query("INSERT INTO `member` (`mem_id`, `mem_pswd`, `mem_name`, `mem_pic`, `mem_profile`, `mem_joinday`, `status`,`mem_area`) VALUES ('$user', '$pass', '$alias', '$pic', '$profile', NOW(), '1','$country')");
		$rs=mysql_affected_rows();
		mysql_close($conn);
		return $rs;
	}
}

function GetFashionDay($day, $country) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	//include '../connect/common_start.php';
	$curDay= date("Y-m-d");
	$NewDate = date('Y-m-d', strtotime($curDay . "$day days"));
	$sql="SELECT p.* FROM post p, store s WHERE  p.date='$NewDate' AND p.type='1' AND p.id_store=s.id AND s.`status`=1 AND s.store_area='$country'";
	//$result=mysql_query("SELECT * FROM post m WHERE (m.date='$NewDate' AND m.status='1')");
	$result=mysql_query($sql);
	$rows = array();
	while($r = mysql_fetch_assoc($result)) {
		$rows[] = $r;
	}
	//include '../connect/common_end.php';
	mysql_close($conn);
	$rs=json_encode($rows);
	return $rs;
}

function GetListProduct($idPost) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	$result=mysql_query("SELECT * FROM product p WHERE (p.id_post='$idPost' AND p.status='1')");

	$row=array();
	while ($r=mysql_fetch_assoc($result)) {
		$row[] = $r;
	}
	mysql_close($conn);
	return  json_encode($row);
}

function GetLikeMember($mem_id,$idPost) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	$result=mysql_query("SELECT SUM(l.`like`) as total FROM `like` l WHERE l.id_mem='$mem_id' AND l.id_post='$idPost'");
	if(mysql_num_rows($result)>0){
		$r = mysql_fetch_assoc($result);
		mysql_close($conn);
		return  json_encode($r);
	}else{
		mysql_close($conn);
		return  '{}';
	}
}

function SetLikePost($mem_id, $post_id, $type) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	$sql="UPDATE `post` SET `likes`=`likes`+'$type' WHERE (`id`='$post_id')";

	$result1=mysql_query($sql);

	$result=mysql_query("INSERT INTO `like` (`id_mem`, `id_post`, `like`, `datetime`) VALUES ('$mem_id', '$post_id', '$type', NOW())");
	$rs=mysql_affected_rows();
	mysql_close($conn);
	return $rs;
}

function GetListCommentPost($post_id) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	$result=mysql_query("SELECT c.id,c.id_mem,c.id_post,c.cm_id,c.cm_content,c.date,m.mem_id as'mem_id',m.mem_name,m.mem_pic FROM `comment` c JOIN member m ON c.id_mem=m.id WHERE (c.id_post='$post_id' AND c.status='1') ORDER BY c.date");

	$row=array();
	while ($r=mysql_fetch_assoc($result)) {
		$row[]=$r;
	}
	mysql_close($conn);
	return json_encode($row);
}

function AddCommentPost($mem, $idPost, $cmid, $cmcontent) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$cmid=urldecode($cmid);
	$cmcontent=urldecode($cmcontent);
	$sql="UPDATE `post` SET `comments`=`comments`+'1' WHERE (`id`='$idPost')";
	$result1=mysql_query($sql);

	$result=mysql_query("INSERT INTO `comment` (`id_mem`, `id_post`, `cm_id`, `cm_content`, `date`, `status`) VALUES ('$mem', '$idPost', '$cmid', '$cmcontent',NOW(),'1')");
	$rs= mysql_affected_rows();
	mysql_close($conn);
	return $rs;
}

function CreateNewPost($mem_id, $year, $month, $day, $title, $price, $link, $image, $content) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	
	//$date = date_create($year.'-'.$month.'-'.$day);
	//$date= date_format($date, 'Y-m-d');
	$date = $year.'-'.$month.'-'.$day;
	//echo $date;
	$title=urldecode($title);
	$price=urldecode($price);
	$link=urldecode($link);
	$content=urldecode($content);
	$ip=$_SERVER['REMOTE_ADDR'];
	$sql="INSERT INTO `post` (`id_store`, `date`, `title`, `price`, `link`, `image`, `content`, `create_day`, `status`, `likes`, `comments`, `type`,`ip`) VALUES ('$mem_id', '$date', '$title', '$price', '$link', '$image', '$content', NOW(), '1','0','0' ,'2','$ip')";
	//echo $sql;
	$result=mysql_query($sql);

	$rs= mysql_affected_rows();
	mysql_close($conn);
	return $rs;
}

function GetInfoStore($store_id) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	$result=mysql_query("SELECT * FROM store s WHERE (s.id='$store_id' AND s.status!='0')");
	if(mysql_num_rows($result)>0){
		$r = mysql_fetch_assoc($result);
		mysql_close($conn);
		return  json_encode($r);
	}else{
		mysql_close($conn);
		return  '{}';
	}
}

function GetInfoFriend($mem_id) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	$sql="SELECT f.id, f.create_date, f.type,m.id as friendid, m.mem_id, m.mem_name, m.mem_pic, m.mem_profile FROM friend f INNER JOIN member m ON f.mem_friend=m.id WHERE f.mem_id='$mem_id' AND f.`status`='1';";
	$result=mysql_query($sql);
	$row=array();
	while ($r=mysql_fetch_assoc($result))
	{
		$row[]=$r;
	}
	mysql_close($conn);
	return  json_encode($row);

}

function AddFriend($mem_id, $mem_id_friend) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	$sql="INSERT INTO `friend` (`mem_id`, `mem_friend`, `type`, `status`, `create_date`) VALUES ('$mem_id', '$mem_id_friend','1', '1', NOW()), ('$mem_id_friend', '$mem_id','0', '1', NOW())";
	$result=mysql_query($sql);
	$rs= mysql_affected_rows();

	mysql_close($conn);
	return $rs;
}

function DeleteFriend($mem_id, $mem_id_friend) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	$sql="DELETE FROM friend WHERE (friend.mem_id='$mem_id' AND friend.mem_friend='$mem_id_friend') OR (friend.mem_id='$mem_id_friend' AND friend.mem_friend='$mem_id')";
	$result=mysql_query($sql);
	$rs= mysql_affected_rows();

	mysql_close($conn);
	return $rs;
}

function GetFashionMember($mem_id) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	$result=mysql_query("SELECT * FROM post m WHERE (m.id_store='$mem_id')");

	$rows = array();
	while($r = mysql_fetch_assoc($result)) {
		$rows[] = $r;
	}
	mysql_close($conn);
	return json_encode($rows);
}

function GetFashionDayAllMember($day) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	//$day=$_REQUEST['day'];
	$curDay= date("Y-m-d");
	$NewDate = date('Y-m-d', strtotime($curDay . "$day days"));
	$result=mysql_query("SELECT * FROM post m WHERE (m.type='2' AND m.date='$NewDate' AND m.status='1')");

	$rows = array();
	while($r = mysql_fetch_assoc($result)) {
		$rows[] = $r;
	}
	mysql_close($conn);
	return json_encode($rows);
}

function GetLikePost($idPost) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	$sql="SELECT SUM(l.`like`) as total FROM `like` l WHERE l.id_post='$idPost'";
	$result=mysql_query($sql);
	if(mysql_num_rows($result)>0){
		$r = mysql_fetch_assoc($result);
		mysql_close($conn);
		return  json_encode($r);
	}else{
		mysql_close($conn);
		return  '{}';
	}

}

function UpdateProfileMember($mem_id,$mem_pass, $profile) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$mem_id=urldecode($mem_id);
	$profile=urldecode($profile);
	$sql="UPDATE `member` SET `mem_profile`='$profile' WHERE (`mem_id`='$mem_id' AND mem_pswd='$mem_pass' AND status='1' )";
	$result=mysql_query($sql);
	$rs= mysql_affected_rows();

	mysql_close($conn);
	return $rs;
}

function UpdateImageMember($mem_id,$mem_pass, $mem_pic) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$mem_id=urldecode($mem_id);
	$sql="UPDATE `member` SET `mem_pic`='$mem_pic' WHERE (`mem_id`='$mem_id' AND mem_pswd='$mem_pass' AND status='1' )";
	$result=mysql_query($sql);
	$rs= mysql_affected_rows();

	mysql_close($conn);
	return $rs;
}

function GetInfoMemberById($id) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	$sql="SELECT * FROM member m WHERE (m.id='$id' AND m.status='1')";
	$result=mysql_query($sql);
	if(mysql_num_rows($result)>0){
		$r = mysql_fetch_assoc($result);
		mysql_close($conn);
		return json_encode($r);
	}else{
		mysql_close($conn);
		return '{}';
	}
}

function AddNotification($id, $mem_id, $notification, $id_post) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$notification=urldecode($notification);
	$sql="INSERT INTO `notification` (`id`, `mem_id`, `notification`, `id_post`) VALUES ('$id', '$mem_id', '$notification','$id_post')";
	$result=mysql_query($sql);
	$rs=mysql_affected_rows();
	mysql_close($conn);
	return  $rs;
}


function DeleteNotification($id) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	$sql="DELETE FROM `notification` WHERE (`id`='$id')";
	$result=mysql_query($sql);
	$rs= mysql_affected_rows();

	mysql_close($conn);
	return  $rs;
}

function GetFashionDayFriend($mem_id,$day) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	$curDay= date("Y-m-d");
	$NewDate = date('Y-m-d', strtotime($curDay . "$day days"));
	$sql="SELECT * FROM post p INNER JOIN
							(SELECT f.mem_friend FROM  friend f 
							WHERE f.mem_id='$mem_id' AND f.`status`='1') as f 
							ON p.id_store=f.mem_friend
			WHERE p.date='$NewDate'";
	$result=mysql_query($sql);
	$rows = array();
	while($r = mysql_fetch_assoc($result)) {
		$rows[] = $r;
	}

	mysql_close($conn);
	return  json_encode($rows);
}

function GetNotification($id) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	$sql="SELECT * FROM `notification` WHERE (`mem_id`='$id')";
	$result=mysql_query($sql);

	$sql2="DELETE FROM `notification` WHERE (`mem_id`='$id')";
	$result2=mysql_query($sql2);

	$row=array();
	while ($r=mysql_fetch_assoc($result))
	{
		$row[]=$r;
	}

	mysql_close($conn);
	return  json_encode($row);
}

function GetCountry($ip) {

	$x = gethostbyname($ip);
	//echo $x."<br>";
	$ip_long=sprintf('%u',ip2long($x));
	//echo $ip_long."<br>";
	$row = 1;
	//include 'getCountry/getCountry.php';
	if (($handle = fopen("getCountry/GeoIPCountryWhois.csv", "r")) !== FALSE) {
		while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
			$num = count($data);
			// echo "<p> $num fields in line $row: <br /></p>\n";
			$row++;
			// for ($c=0; $c < $num; $c++) {

			//     echo $data[$c] . "<br />\n";

			// }
			if($data[2]<= $ip_long && $ip_long <= $data[3]){
				$rs= $data[4];
				break;
			}
		}
		fclose($handle);
	}
	return $rs;
}

function UpdateNameMember($mem_id,$mem_pass, $mem_name) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	$mem_id=urldecode($mem_id);
	$mem_name=urldecode($mem_name);
	$sql="UPDATE `member` SET `mem_name`='$mem_name' WHERE (`mem_id`='$mem_id' AND mem_pswd='$mem_pass' AND status='1' )";
	$result=mysql_query($sql);
	$rs= mysql_affected_rows();

	mysql_close($conn);
	return $rs;
}

function UpdateMember($mem_id,$mem_pass, $mem_name, $mem_profile) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$mem_id=urldecode($mem_id);
	$mem_name=urldecode($mem_name);
	$mem_profile=urldecode($mem_profile);
	$sql="UPDATE `member` SET `mem_name`='$mem_name',`mem_profile`='$mem_profile' WHERE (`mem_id`='$mem_id' AND mem_pswd='$mem_pass' AND status='1' )";
	$result=mysql_query($sql);
	$rs= mysql_affected_rows();

	mysql_close($conn);
	return $rs;
}

function GetFashionDayLike($day, $country, $member) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	//include '../connect/common_start.php';
	$curDay= date("Y-m-d");
	$NewDate = date('Y-m-d', strtotime($curDay . "$day days"));
	$sql="SELECT p.*,((SELECT SUM(l.`like`) as total FROM `like` l WHERE l.id_mem='$member' AND l.id_post=p.id)) as islike FROM post p, store s WHERE  p.date='$NewDate' AND p.type='1' AND p.`status`=1 AND p.id_store=s.id AND s.`status`=1 AND s.store_area='$country'";
	//$result=mysql_query("SELECT * FROM post m WHERE (m.date='$NewDate' AND m.status='1')");
	$result=mysql_query($sql);
	$rows = array();
	while($r = mysql_fetch_assoc($result)) {
		$rows[] = $r;
	}
	//include '../connect/common_end.php';
	mysql_close($conn);
	$rs=json_encode($rows);
	return $rs;
}

function GetFashionMemberLike($mem_id,$country) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	//$sql="SELECT p.*,((SELECT SUM(l.`like`) as total FROM `like` l WHERE l.id_mem='$mem_id' AND l.id_post=p.id)) as islike
	//	FROM post p WHERE (p.id_store='$mem_id' AND p.`status`=1)";
	$sql="SELECT p.*,((SELECT SUM(l.`like`) as total FROM `like` l WHERE l.id_mem='$mem_id' AND l.id_post=p.id)) as islike
		FROM post p,member m WHERE (p.id_store='$mem_id' AND p.`status`=1 AND p.id_store=m.id AND m.mem_area='$country') ORDER BY p.create_day DESC";
	$result=mysql_query($sql);

	$rows = array();
	while($r = mysql_fetch_assoc($result)) {
		$rows[] = $r;
	}
	mysql_close($conn);
	return json_encode($rows);
}

function GetFashionDayAllMemberLike($day,$mem_id,$country) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	//$day=$_REQUEST['day'];
	//echo '1111'.$day;
	$curDay= date("Y-m-d");
	//echo '2222'.$curDay;
	$NewDate = date('Y-m-d', strtotime($curDay . "$day days"));



	//echo '3333'.$NewDate;
	//$sql="SELECT p.*,((SELECT SUM(l.`like`) as total FROM `like` l WHERE l.id_mem='$mem_id' AND l.id_post=p.id)) as islike
	//	FROM post p WHERE (p.type='2' AND p.date='$NewDate' AND p.status='1')";
	
	$sql="SELECT p.*,((SELECT SUM(l.`like`) as total FROM `like` l WHERE l.id_mem='$mem_id' AND l.id_post=p.id)) as islike
		FROM post p,member m WHERE (p.type='2' AND p.status='1' AND p.id_store=m.id AND m.mem_area='$country') ORDER BY p.create_day DESC";
	
	//echo $sql;
	$result=mysql_query($sql);

	$rows = array();
	while($r = mysql_fetch_assoc($result)) {
		$rows[] = $r;
	}
	//echo $rows;
	mysql_close($conn);
	return json_encode($rows);
}


function GetFashionDayFriendLike($mem_id,$day,$country) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	$curDay= date("Y-m-d");
	$NewDate = date('Y-m-d', strtotime($curDay . "$day days"));
	/*$sql="SELECT *,((SELECT SUM(l.`like`) as total FROM `like` l WHERE l.id_mem='$mem_id' AND l.id_post=p.id)) as islike FROM post p INNER JOIN
							(SELECT f.mem_friend FROM  friend f 
							WHERE f.mem_id='$mem_id' AND f.`status`='1') as f 
							ON p.id_store=f.mem_friend
			WHERE p.date='$NewDate' AND p.`status`=1" ;*/
	$sql="SELECT p.*,((SELECT SUM(l.`like`) as total FROM `like` l WHERE l.id_mem='$mem_id' AND l.id_post=p.id)) as islike 
				FROM (post p INNER JOIN
							(SELECT f.mem_friend FROM  friend f 
							WHERE f.mem_id='$mem_id' AND f.`status`='1') as f 
							ON p.id_store=f.mem_friend),member m
			WHERE (p.`status`=1 AND p.`type`='2' AND p.id_store=m.id AND m.mem_area='$country') ORDER BY p.create_day DESC" ;
	$result=mysql_query($sql);
	$rows = array();
	while($r = mysql_fetch_assoc($result)) {
		$rows[] = $r;
	}

	mysql_close($conn);
	return  json_encode($rows);
}

function TestUTF8($value) {
	$v=urldecode($value);
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	$sql="UPDATE `member` SET `mem_profile`='$v' WHERE (`mem_id`='hung' AND mem_pswd='hung' AND status='1' )";
	$result=mysql_query($sql);
	$rs= mysql_affected_rows();

	mysql_close($conn);
	return $rs;
	//return  $v;
}

function GetFashionAllMemberLike($mem_id,$country) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	//$sql="SELECT p.*,((SELECT SUM(l.`like`) as total FROM `like` l WHERE l.id_mem='$mem_id' AND l.id_post=p.id)) as islike
	//	FROM post p WHERE (p.id_store='$mem_id')";
	$sql="SELECT p.*,((SELECT SUM(l.`like`) as total FROM `like` l WHERE l.id_mem='$mem_id' AND l.id_post=p.id)) as islike
		FROM post p,member m WHERE (p.id_store='$mem_id' AND p.`status`=1 AND p.id_store=m.id AND m.mem_area='$country') ORDER BY p.create_day DESC";
	
	$result=mysql_query($sql);

	$rows = array();
	while($r = mysql_fetch_assoc($result)) {
		$rows[] = $r;
	}
	mysql_close($conn);
	return json_encode($rows);
}

function DeleteFashionMember($mem_id,$id_post) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$sql="UPDATE `post` SET `status`='2' WHERE (`id`='$id_post' AND `id_store`='$mem_id' AND `type`='2')";
	$result=mysql_query($sql);
	$rs= mysql_affected_rows();

	mysql_close($conn);
	return  $rs;
}

function UpdateFashionMember($mem_id,$id_post,$image,$content) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");	
	$curDay= date("Y-m-d");
	//$NewDate = date('Y-m-d', strtotime($curDay . "$day days"));	
	$content=urldecode($content);
	//$sql="UPDATE `post` SET `date`='$curDay', `image`='$image', `content`='$content' WHERE (`id`='$id_post' AND `id_store`='$mem_id' AND `type`='2')";
	$sql="UPDATE `post` SET `date`='$curDay',`create_day`=NOW(), `image`='$image', `content`='$content' WHERE (`id`='$id_post' AND `id_store`='$mem_id' AND `type`='2' AND `status`='1')";
	$result=mysql_query($sql);
	$rs= mysql_affected_rows();

	mysql_close($conn);
	return  $rs;
}

function GetFashionMemberAndMyLike($mem_id,$my_mem_id,$country) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	//$sql="SELECT p.*,((SELECT SUM(l.`like`) as total FROM `like` l WHERE l.id_mem='$mem_id' AND l.id_post=p.id)) as islike
	//	FROM post p WHERE (p.id_store='$mem_id' AND p.`status`=1)";
	$sql="SELECT p.*,((SELECT SUM(l.`like`) as total FROM `like` l WHERE l.id_mem='$my_mem_id' AND l.id_post=p.id)) as islike
		FROM post p,member m WHERE (p.id_store='$mem_id' AND p.`status`=1 AND p.id_store=m.id AND m.mem_area='$country') ORDER BY p.create_day DESC";
	$result=mysql_query($sql);

	$rows = array();
	while($r = mysql_fetch_assoc($result)) {
		$rows[] = $r;
	}
	mysql_close($conn);
	return json_encode($rows);
}

function GetPostLikeMember($mem_id) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	//$sql="SELECT p.*,((SELECT SUM(l.`like`) as total FROM `like` l WHERE l.id_mem='$mem_id' AND l.id_post=p.id)) as islike
	//	FROM post p WHERE (p.id_store='$mem_id' AND p.`status`=1)";
	$sql="SELECT p.*
			FROM post p,`like` l 
			WHERE (l.id_mem='$mem_id' AND p.id=l.id_post AND p.`status`='1' AND ((SELECT SUM(l.`like`) as total FROM `like` l WHERE l.id_mem='$mem_id' AND l.id_post=p.id) >0)) 
			ORDER BY p.create_day DESC";
	$result=mysql_query($sql);

	$rows = array();
	while($r = mysql_fetch_assoc($result)) {
		$rows[] = $r;
	}
	mysql_close($conn);
	return json_encode($rows);
}

function GetFashionDayAllMemberLikePage($day,$mem_id,$country,$start,$end) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	$curDay= date("Y-m-d");
	$NewDate = date('Y-m-d', strtotime($curDay . "$day days"));
	$sql="SELECT p.*,((SELECT SUM(l.`like`) as total FROM `like` l WHERE l.id_mem='$mem_id' AND l.id_post=p.id)) as islike
		FROM post p,member m WHERE (p.type='2' AND p.status='1' AND p.id_store=m.id AND m.mem_area='$country')
		 ORDER BY p.create_day DESC
		  LIMIT $start,$end";
	$result=mysql_query($sql);
	$rows = array();
	while($r = mysql_fetch_assoc($result)) {
		$rows[] = $r;
	}
	//echo $rows;
	mysql_close($conn);
	return json_encode($rows);
}

function GetFashionDayLikePage($day, $country, $member,$start,$end) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	//include '../connect/common_start.php';
	$curDay= date("Y-m-d");
	$NewDate = date('Y-m-d', strtotime($curDay . "$day days"));
	$sql="SELECT p.*,((SELECT SUM(l.`like`) as total FROM `like` l WHERE l.id_mem='$member' AND l.id_post=p.id)) as islike 
			FROM post p, store s WHERE  (p.date='$NewDate' AND p.type='1' AND p.`status`=1 AND p.id_store=s.id AND s.`status`=1 AND s.store_area='$country')
			 ORDER BY p.create_day ASC 
			 LIMIT $start,$end";
	//$result=mysql_query("SELECT * FROM post m WHERE (m.date='$NewDate' AND m.status='1')");
	$result=mysql_query($sql);
	$rows = array();
	while($r = mysql_fetch_assoc($result)) {
		$rows[] = $r;
	}
	//include '../connect/common_end.php';
	mysql_close($conn);
	$rs=json_encode($rows);
	return $rs;
}

function GetFashionDayFriendLikePage($mem_id,$day,$country,$start,$end) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	$curDay= date("Y-m-d");
	$NewDate = date('Y-m-d', strtotime($curDay . "$day days"));
	/*$sql="SELECT *,((SELECT SUM(l.`like`) as total FROM `like` l WHERE l.id_mem='$mem_id' AND l.id_post=p.id)) as islike FROM post p INNER JOIN
							(SELECT f.mem_friend FROM  friend f 
							WHERE f.mem_id='$mem_id' AND f.`status`='1') as f 
							ON p.id_store=f.mem_friend
			WHERE p.date='$NewDate' AND p.`status`=1" ;*/
	$sql="SELECT p.*,((SELECT SUM(l.`like`) as total FROM `like` l WHERE l.id_mem='$mem_id' AND l.id_post=p.id)) as islike 
				FROM (post p INNER JOIN
							(SELECT f.mem_friend FROM  friend f 
							WHERE f.mem_id='$mem_id' AND f.`status`='1') as f 
							ON p.id_store=f.mem_friend),member m
			WHERE (p.`status`=1 AND p.`type`='2' AND p.id_store=m.id AND m.mem_area='$country') ORDER BY p.create_day DESC LIMIT $start,$end" ;
	$result=mysql_query($sql);
	$rows = array();
	while($r = mysql_fetch_assoc($result)) {
		$rows[] = $r;
	}

	mysql_close($conn);
	return  json_encode($rows);
}

function GetFashionMemberAndMyLikePage($mem_id,$my_mem_id,$country,$start,$end) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");

	//$sql="SELECT p.*,((SELECT SUM(l.`like`) as total FROM `like` l WHERE l.id_mem='$mem_id' AND l.id_post=p.id)) as islike
	//	FROM post p WHERE (p.id_store='$mem_id' AND p.`status`=1)";
	$sql="SELECT p.*,((SELECT SUM(l.`like`) as total FROM `like` l WHERE l.id_mem='$my_mem_id' AND l.id_post=p.id)) as islike
		FROM post p,member m WHERE (p.id_store='$mem_id' AND p.`status`=1 AND p.id_store=m.id AND m.mem_area='$country') ORDER BY p.create_day DESC LIMIT $start,$end";
	$result=mysql_query($sql);

	$rows = array();
	while($r = mysql_fetch_assoc($result)) {
		$rows[] = $r;
	}
	mysql_close($conn);
	return json_encode($rows);
}

function GetThumbnailFashion($id_store,$day,$countday) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	$conn = mysql_connect($hostname, $username, $password) or
	die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	
	$sDay= date("Y-m-d");
	//$NewDate = date('Y-m-d', strtotime($curDay . "$day days"));
	$curDate = date('Y-m-d', strtotime($sDay . "$day days"));
	//echo $curDate;
	$oldDate = date('Y-m-d', strtotime($curDate . "$countday days"));
	//echo $oldDate;
	$sql="SELECT * FROM post p
		WHERE p.id_store='$id_store' AND p.`status`='1' AND p.date<'$curDate' AND p.date>='$oldDate'
		ORDER BY p.date ASC";
	$result=mysql_query($sql);

	$rows = array();
	while($r = mysql_fetch_assoc($result)) {
		$rows[] = $r;
	}
	mysql_close($conn);
	return json_encode($rows);
}

$server = new soap_server();
$server->register("LogInMember");
$server->register("GetInfoMember");
$server->register("ChangePasswordMember");
$server->register("CreateNewMember");
$server->register("GetFashionDay");
$server->register("GetListProduct");
$server->register("GetLikeMember");
$server->register("SetLikePost");
$server->register("GetListCommentPost");
$server->register("AddCommentPost");
$server->register("CreateNewPost");
$server->register("GetInfoStore");
$server->register("GetInfoFriend");
$server->register("AddFriend");
$server->register("DeleteFriend");
$server->register("GetFashionMember");
$server->register("GetFashionDayAllMember");
$server->register("GetLikePost");
$server->register("UpdateProfileMember");
$server->register("UpdateImageMember");
$server->register("GetInfoMemberById");
$server->register("AddNotification");
$server->register("DeleteNotification");		//khong dung
$server->register("GetFashionDayFriend");
$server->register("GetNotification");
$server->register("GetCountry");				//Get country
$server->register("UpdateNameMember");
$server->register("UpdateMember");
$server->register("GetFashionDayLike");
$server->register("GetFashionMemberLike");
$server->register("GetFashionDayAllMemberLike");
$server->register("GetFashionDayFriendLike");
$server->register("GetFashionAllMemberLike");
$server->register("DeleteFashionMember");
$server->register("UpdateFashionMember");
$server->register("GetFashionMemberAndMyLike");
$server->register("GetPostLikeMember");
$server->register("GetFashionDayAllMemberLikePage");
$server->register("GetFashionDayLikePage");
$server->register("GetFashionDayFriendLikePage");
$server->register("GetFashionMemberAndMyLikePage");
$server->register("GetThumbnailFashion");
$server->register("TestUTF8");

$server->service($HTTP_RAW_POST_DATA);